package com.example.guiguiapplicatio.fragment.SigningArchives;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.style.FontStyle;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.FamilySigningdetail;
import com.example.guiguiapplicatio.Response.Signingdetail;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.example.guiguiapplicatio.utils.ApiUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import cn.refactor.lib.colordialog.PromptDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SigningdetailFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private ApiUtils apiUtils;
    private String idCard;
    private String signingId;
    ImageButton servic_back;
    TextView et_ethnic;
    TextView et_phone;
    TextView et_detailedresidence;
    TextView et_Profession;
    TextView et_marriage;
    SmartTable table;
    Button comit_btu1;
    Button comit_btu4;
    ImageView imageView;
    Bitmap bitmap;
    String imageUrl;
    private List<FamilySigningdetail> list = new ArrayList<>();
    ;

    public SigningdetailFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PoorFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SigningdetailFragment newInstance(String param1, String param2) {
        SigningdetailFragment fragment = new SigningdetailFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            idCard = getArguments().getString("idCard");
            signingId = getArguments().getString("signingId");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_total_detail_contract_detailed, container, false);
        servic_back = root.findViewById(R.id.servic_back);
        et_ethnic = root.findViewById(R.id.et_ethnic);
        et_phone = root.findViewById(R.id.et_phone);
        et_detailedresidence = root.findViewById(R.id.et_detailedresidence);
        et_Profession = root.findViewById(R.id.et_Profession);
        et_marriage = root.findViewById(R.id.et_marriage);
        comit_btu1 = root.findViewById(R.id.comit_btu1);
        comit_btu4 = root.findViewById(R.id.comit_btu4);
        table = root.findViewById(R.id.table);
        apiUtils = new ApiUtils(getString(R.string.api));
        Call<ResponseBody<Signingdetail>> call = apiUtils.getApi().getSigningdetail(Integer.parseInt(signingId));
        call.clone().enqueue(new Callback<ResponseBody<Signingdetail>>() {
            @Override
            public void onResponse(Call<ResponseBody<Signingdetail>> call, Response<ResponseBody<Signingdetail>> response) {
                ResponseBody<Signingdetail> responseBody = response.body();
                if (responseBody != null && responseBody.getData() != null) {
                    Signingdetail signingdetail = responseBody.getData();
                    Log.d("1111111111111111", responseBody.toString());
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            et_ethnic.setText(signingdetail.getHouseholdName());
                            et_phone.setText(signingdetail.getHouseholdPhone());
                            et_detailedresidence.setText(signingdetail.getHomeAddress());
                            et_Profession.setText(signingdetail.getOrganization());
                            et_marriage.setText(signingdetail.getDoctorName());
                            list = signingdetail.getFamilySigningdetailList();
                            imageUrl = signingdetail.getServicebook();
                            table.setData(list);
                            table.getConfig()
                                    .setShowXSequence(false)
                                    .setColumnTitleStyle(new FontStyle(50, Color.BLACK))
                                    .setShowYSequence(false)
                                    .setShowTableTitle(false)
                                    .setContentStyle(new FontStyle(50, Color.BLUE));
// 创建一个新线程来下载图片
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<ResponseBody<Signingdetail>> call, Throwable t) {

            }
        });
        servic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("idCard", idCard);
                // 使用 NavController 导航到目标 fragment，并将 Bundle 作为参数传递
                Navigation.findNavController(v).navigate(R.id.action_SigningdetailFragment_to_SigningArchivesFragment, bundle);
            }
        });
        comit_btu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("idCard", idCard);
                bundle.putString("imageUrl", imageUrl);
                bundle.putString("signingId", signingId);
                // 使用 NavController 导航到目标 fragment，并将 Bundle 作为参数传递
                Navigation.findNavController(v).navigate(R.id.action_SigningdetailFragment_to_ServicebookFragment, bundle);
            }
        });
        comit_btu4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("解约").setMessage("该操作不可逆，是否解约？").setNegativeButton("否", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface arg0, int arg1) {
                                arg0.dismiss();
                            }
                        })
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                Call<ResponseBody<Object>> responseBodyCall = apiUtils.getApi().deleteSigning(Integer.parseInt(signingId));
                                responseBodyCall.clone().enqueue(new Callback<ResponseBody<Object>>() {
                                    @Override
                                    public void onResponse(Call<ResponseBody<Object>> call, Response<ResponseBody<Object>> response) {
                                        ResponseBody<Object> responseBody = response.body();
                                        if (responseBody != null && responseBody.getCode() == 200) {
                                            getActivity().runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    new PromptDialog(getContext())
                                                            .setDialogType(PromptDialog.DIALOG_TYPE_SUCCESS)
                                                            .setAnimationEnable(true)
                                                            .setTitleText("SUCCESS")
                                                            .setContentText("解约成功")
                                                            .setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
                                                                @Override
                                                                public void onClick(PromptDialog dialog) {
                                                                    Bundle bundle = new Bundle();
                                                                    bundle.putString("idCard", idCard);
                                                                    // 使用 NavController 导航到目标 fragment，并将 Bundle 作为参数传递
                                                                    Navigation.findNavController(v).navigate(R.id.action_SigningdetailFragment_to_SigningArchivesFragment, bundle);
                                                                    dialog.dismiss();
                                                                }
                                                            }).show();
                                                }
                                            });
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<ResponseBody<Object>> call, Throwable t) {

                                    }
                                });
                                /*提醒测试作用*/
                            }
                        });
                builder.create().show();
            }
        });
        return root;
    }

    @Override
    public void onResume() {
        BottomNavigationView bottomNavigationView = requireActivity().findViewById(R.id.nav_view);
        bottomNavigationView.setVisibility(View.INVISIBLE);
        //这两行代码设置隐藏底部导航栏，若要显示啧修改INVISIBLE为Visible（可见）
        super.onResume();
    }

}
